///////    Cirbuf.java    ///////

public class Cirbuf {
	public Cirbuf() {
		
		this(D_SIZE);
		System.out.println("zoro edit this");
	} // constructor call
	public Cirbuf(String str,boolean b){
		//zoro ....
		//roy ....
	}
	public Cirbuf(int s) {
		head = tail = length = 0;
		fin = false;
		size = s;
		cb = new char[s];
		//zoro added this
	}

	public synchronized boolean is_empty() {
		return length == 0;
		//roy added this
	}

	public synchronized boolean is_full() {
		return length == size;
	}

	public synchronized boolean finished() {
		return fin;
	}

	public synchronized void finished(boolean v) {
		fin = v;
		//notify();
	}

	public synchronized void put(char c) {
		while (is_full())
			try {
				wait(); // wait on monitor
			} catch (InterruptedException e) {
			}
		cb[tail++] = c;
		System.out.println("__put "+c);
		length++;
		tail = mod(tail);
		notify(); // resume waiting consumer thread
	}

	public synchronized boolean get(char c[]) {
		while (is_empty()) {
			if (fin)
				return (false);
			else
				try {
					wait(); // wait on monitor
				} catch (InterruptedException e) {
				}
		}
		c[0] = cb[head++];
		System.out.println("get__ "+c[0]);
		length--;
		head = mod(head);
		notify(); // resume waiting producer thread
		return true;
	}

	private static final int D_SIZE = 16;
	private volatile boolean fin = false;
	private volatile int head; // first char in buffer
	private volatile int tail; // first empty slot in buffer
	private volatile int length; // number of characters in buffer
	private int size; // capacity of buffer
	private char[] cb; // character buffer

	private int mod(int x) {
		return (x >= size ? x - size : x);
	}
}
